home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / file.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  4.6 KB  |  223 lines  |  [TEXT/ttxt]

  1. /*
  2.  
  3.    file.c
  4.  
  5.    This software is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public
  7.    License as published by the Free Software Foundation; either
  8.    version 2 of the License, or (at your option) any later version.
  9.  
  10.    This software is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with this software; if not, write to the Free
  17.    Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  
  19.    Original copyright notice follows:
  20.  
  21.    Copyright, 1993, Brent Benson.  All Rights Reserved.
  22.    0.4 & 0.5 Revisions Copyright 1994, Joseph N. Wilson.  All Rights Reserved.
  23.  
  24.    Permission to use, copy, and modify this software and its
  25.    documentation is hereby granted only under the following terms and
  26.    conditions.  Both the above copyright notice and this permission
  27.    notice must appear in all copies of the software, derivative works
  28.    or modified version, and both notices must appear in supporting
  29.    documentation.  Users of this software agree to the terms and
  30.    conditions set forth in this notice.
  31.  
  32.  */
  33.  
  34. #include <stdio.h>
  35.  
  36. #include "file.h"
  37.  
  38. #include "dylan_lexer.h"
  39. #include "env.h"
  40. #include "eval.h"
  41. #include "error.h"
  42. #include "foreign_ptr.h"
  43. #include "list.h"
  44. #include "parse.h"
  45. #include "prim.h"
  46. #include "read.h"
  47.  
  48. extern Object open_file_list;
  49.  
  50. /* primitives */
  51. static Object infix ();
  52. static Object prefix ();
  53.  
  54. static struct primitive file_prims[] =
  55. {
  56.     {"load", prim_1, load},
  57.     {"i-load", prim_1, i_load},
  58.     {"p-load", prim_1, p_load},
  59.     {"infix", prim_0, infix},
  60.     {"prefix", prim_0, prefix},
  61. };
  62.  
  63. /* function definitions */
  64. static FILE *
  65.   open_file (Object filename);
  66.  
  67. static void
  68.   close_file (FILE * fp);
  69.  
  70. void
  71. init_file_prims (void)
  72. {
  73.     int num;
  74.  
  75.     num = sizeof (file_prims) / sizeof (struct primitive);
  76.  
  77.     init_prims (num, file_prims);
  78. }
  79.  
  80. Object
  81. p_load (Object filename)
  82. {
  83.     FILE *fp;
  84.     Object obj, res;
  85.     struct module_binding *old_module;
  86.  
  87.     old_module = current_module ();
  88.  
  89.     fp = open_file (filename);
  90.  
  91.     while ((obj = read_object (fp)) != eof_object) {
  92.     res = eval (obj);
  93.     }
  94.  
  95.     set_module (old_module);
  96.     close_file (fp);
  97.     return (res);
  98. }
  99.  
  100. Object
  101. i_load (Object filename)
  102. {
  103.     FILE *fp;
  104.     Object obj, res;
  105.     struct module_binding *old_module;
  106.     Object expr_list = make_empty_list ();
  107.     Object *expr_list_ptr;
  108.     int old_load_file_context;
  109.  
  110.     /* save current states. */
  111.     old_load_file_context = load_file_context;
  112.     load_file_context = 1;
  113.     old_module = current_module ();
  114.  
  115.     fp = open_file (filename);
  116.  
  117.     reset_parser (fp);
  118.  
  119.     /*
  120.      * Cons up the list of expressions in the file, then eval them.
  121.      */
  122.  
  123.     expr_list_ptr = &expr_list;
  124.     while ((obj = parse_object (fp, 0)) && (obj != eof_object)) {
  125.     *expr_list_ptr = cons (obj, make_empty_list ());
  126.     expr_list_ptr = &CDR (*expr_list_ptr);
  127.     }
  128.  
  129.     close_file (fp);
  130.  
  131.     while (PAIRP (expr_list)) {
  132.     res = eval (CAR (expr_list));
  133.     expr_list = CDR (expr_list);
  134.     }
  135.  
  136.     set_module (old_module);
  137.     load_file_context = old_load_file_context;
  138.  
  139.     return (res);
  140. }
  141.  
  142. Object
  143. load (Object filename)
  144. {
  145.     Object res;
  146.  
  147.     if (classic_syntax) {
  148.     res = p_load (filename);
  149.     } else {
  150.     FILE *old_yyin = yyin;
  151.  
  152.     res = i_load (filename);
  153.     if (old_yyin == stdin) {
  154.         clearerr (stdin);
  155.         fflush (stdin);
  156.         yyrestart (stdin);
  157.     }
  158.     }
  159.     return res;
  160. }
  161.  
  162. static Object
  163. infix ()
  164. {
  165. #ifdef MACOS
  166.     classic_syntax = 0;
  167.  
  168.     if (yyin == stdin) {
  169.     clearerr (stdin);
  170.     fflush (stdin);
  171.     yyrestart (stdin);
  172.     }
  173. #else
  174.     classic_syntax = -1;
  175.     reset_parser (yyin);
  176. #endif
  177.     return unspecified_object;
  178. }
  179.  
  180. static Object
  181. prefix ()
  182. {
  183.     classic_syntax = 1;
  184.     return unspecified_object;
  185. }
  186.  
  187. static FILE *
  188. open_file (Object filename)
  189. {
  190.     char *str;
  191.     FILE *fp;
  192.  
  193.     if (!BYTESTRP (filename)) {
  194.     error ("load: argument must be a filename", filename, NULL);
  195.     }
  196.     str = BYTESTRVAL (filename);
  197.     fp = fopen (str, "r");
  198.     if (!fp) {
  199.     error ("load: cannot open file", filename, NULL);
  200.     }
  201.     open_file_list = cons (make_foreign_ptr (fp), open_file_list);
  202.     return fp;
  203. }
  204.  
  205. static void
  206. close_file (FILE * fp)
  207. {
  208.     if ((FILE *) FOREIGNPTR (CAR (open_file_list)) != fp) {
  209.     error ("close-file called with bogus fp (BUG)", NULL);
  210.     }
  211.     fclose (fp);
  212.     open_file_list = CDR (open_file_list);
  213. }
  214.  
  215. void
  216. close_open_files (void)
  217. {
  218.     while (PAIRP (open_file_list)) {
  219.     fclose ((FILE *) FOREIGNPTR (CAR (open_file_list)));
  220.     open_file_list = CDR (open_file_list);
  221.     }
  222. }
  223.